<?php
namespace Plu\Library\Services\Redis\Admire;

use Plu\Library\Services\ServiceBase;

class AdmireRedis extends ServiceBase{

    const key_of_admire_day     = "admire:%s:%s";
    const key_of_admire         = "admire:%s:all";
    const key_of_chance_admire  = "admire:chance:%s:%s";


    private $redis;

    public function __construct() {
        $this->redis = $this->getRedis("vector");
    }

    /**
     * @param $type
     * @return \Generator
     * 从redis中获取当天的点赞数值
     */
    public function getAdmireByType($hash_key ,$is_del_key =false) {

        $hash_len = $this->redis->hLen($hash_key);
        if ($hash_len<500) {
            $admires = $this->redis->hGetAll($hash_key);
            yield $admires;
        }else{
            $redis = $this->redis->getRedis();
            $redis->setOption(\Redis::OPT_SCAN, \Redis::SCAN_RETRY);
            $it = null;
            while ($admires = $redis->hScan($hash_key, $it, null, 500)) {
                yield $admires;
            }
        }
        if(!$is_del_key){
            $this->redis->del($hash_key);
        }
    }

    /**
     * @param $mediaId
     * @param $uid
     * @param $type
     * @return bool
     * 判读该用户的是否点赞过
     * 加入是否允许重复点赞的判断
     */
    public function isAdmired($guid, $uid, $type,$is_reAdmire =true) {
        if(!$is_reAdmire){
            return true;
        }
        $hash_key = sprintf(self::key_of_chance_admire, $type, $guid);

        $admire_v = $guid . "_" . $uid;

        return $this->redis->sAdd($hash_key, $admire_v);
    }

    /**
     * @param $guid
     * @param $uid
     * @param $type
     * @return bool
     * 向redis中加入点赞记录
     */
    public function increase($guid, $uid, $type,$is_reAdmire =true) {
        $hash_key = sprintf(self::key_of_admire, $type);
        $hash_key_day = sprintf(self::key_of_admire_day, $type, date("ymd"));

        if ($this->isAdmired($guid, $uid, $type,$is_reAdmire)) {
            $this->redis->hIncrBy($hash_key_day, $guid, 1);
            $this->redis->hIncrBy($hash_key, $guid, 1);

            return true;
        }

        return false;
    }

    public function getCountByGuid($guid, $type){
        $hash_key = sprintf(self::key_of_admire, $type);

        return $this->redis->hGet($hash_key,$guid);
    }

    /**
     * @param $guid
     * @param $uid
     * @param $type
     * @return mixed
     * 判断用户是否点赞
     */
    public function checkAdmireByUid($guid, $uid, $type){
        $hash_key = sprintf(self::key_of_chance_admire, $type, $guid);

        $admire_v = $guid . "_" . $uid;

        return $this->redis->sIsMember($hash_key,$admire_v);
    }

    /**
     * 对新上传的视频进行初始化操作
     */
    public function setValueByType($type,$guid){
        $is_admire_all = $this->getCountByGuid($guid,$type);
        if($is_admire_all !== false){
            return false;
        }else{
            $hash_key = sprintf(self::key_of_admire, $type);
            $hash_key_day = sprintf(self::key_of_admire_day, $type, date("ymd"));
            $this->redis->hSet($hash_key, $guid, 0);
            $this->redis->hSet($hash_key_day, $guid, 0);
            return true;
        }
    }

    public function setAdmiresByGuid($type,$guid,$count){
        $hash_key = sprintf(self::key_of_admire, $type);
        $this->redis->hSet($hash_key, $guid, $count);
    }
}
?>
